import Vue from 'vue'
import VueRouter from 'vue-router'
// const originalPush = VueRouter.prototype.push;
// VueRouter.prototype.push = function push(location) {
//     return originalPush.call(this, location).catch(err => err)
// };

const Home = () =>
    import ('@/views/Home.vue')

const User = () =>
    import ('@/views/user/User.vue')
const Login = () =>
    import ('@/views/user/Login.vue')
const Profile = () =>
    import ('@/views/user/Profile.vue')

const About = () =>
    import ('@/views/About.vue')

const Utils = () =>
    import ('@/views/utils/Utils.vue')

// Alarm his
const Alarm = () =>
    import ('@/views/Alarm.vue')
const AlarmCfg = () =>
    import ('@/views/AlarmCfg.vue')

// alarm cfg
const Test = () =>
    import ('@/views/test/Test.vue')
const Test2 = () =>
    import ('@/views/test/Test.vue')
const Apitest = () =>
    import ('@/views/apitest/ApiTest.vue')

const Task = () =>
    import ('@/views/Task.vue')
const Issue = () =>
    import ('@/views/Issue.vue')
const Requirement = () =>
    import ('@/views/Requirement.vue')
const Risk = () =>
    import ('@/views/Risk.vue')


// routes
const utils = {
    path: '/utils',
    meta: { title: 'Utils' },
    name: 'Utils',
    component: Utils
}

const login = {
    path: '/Login',
    meta: { title: 'Login' },
    name: 'Login',
    component: Login,
}

const user = {
    path: '/user',
    meta: { title: 'User' },
    name: 'User',
    component: User,
    redirect: '/user/profile',
    children: [{
        path: 'profile/',
        meta: { title: 'Profile' },
        component: Profile,
    }, ]
}
const about = {
    path: '/about',
    meta: { title: 'About' },
    name: 'About',
    component: About
}
const test = {
    path: '/test',
    meta: { title: 'Test' },
    name: 'Test',
    component: Test
}
const apitest = {
    path: '/apitest',
    meta: { title: 'ApiTest' },
    name: 'ApiTest',
    component: Apitest
}
const test2 = {
    path: '/test2',
    meta: { title: 'Test' },
    name: 'Test2',
    component: Test2
}


const alarm = {
    path: '/alarm',
    meta: { title: 'AlarmHis' },
    component: Alarm,
}

const alarmcfg = {
    path: '/alarmcfg',
    meta: { title: 'AlarmCfg' },
    component: AlarmCfg
}
const task = {
    path: '/task',
    meta: { title: 'Task' },
    component: Task
}
const issue = {
    path: '/issue',
    meta: { title: 'Issue' },
    component: Issue
}
const requirement = {
    path: '/requirement',
    meta: { title: 'Requirement' },
    component: Requirement
}
const risk = {
    path: '/risk',
    meta: { title: 'Risk' },
    component: Risk
}
const dashboard = {
    path: '/dashboard',
    meta: { title: 'Dashboard' },
    component: () =>
        import ('@/views/Dashboard.vue')
}



const routes = [{
        path: '/',
        meta: { title: 'Login' },
        redirect: 'login',
    },
    {
        path: '/home',
        meta: { title: 'Home' },
        name: 'Home',
        component: Home,
        children: [,
            test,
            test2,
            apitest,
            utils,
            about,
            user,
            alarm,
            alarmcfg,
            risk,
            issue,
            requirement,
            task,
            dashboard

        ]
    },
    login,

]


Vue.use(VueRouter)


const router = new VueRouter({
    routes,
    mode: 'history'
})


export default router

router.beforeEach((to, from, next) => {
    document.title = to.meta.title
    if (to.path === '/login') {
        next();
    } else {
        let token = localStorage.getItem('Authorization');
        if (token === 'null' || token === '') {
            next('/login');
        } else {
            next();
        }
    }
});

// router.afterEach(function(to, form) {
//     // this.store.commit('changeCurPath')
// })